package com.gxwgy.wg2404.controller;

import com.gxwgy.wg2404.util.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author 姚相国
 * @version 1.0
 * @className CreateEventServlet 此类创建活动的
 * @date 2024/12/18 04:26
 */
@WebServlet("/organizer/createEvent")
public class CreateEventServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求编码，防止中文乱码
        request.setCharacterEncoding("UTF-8");

        // 获取当前登录用户的 session
        HttpSession session = request.getSession();
        Integer userId = (Integer) session.getAttribute("userId");
        String username = (String) session.getAttribute("username");// 确保登录时已设置此属性

        if (username == null) {
            response.sendRedirect(request.getContextPath() + "/index.jsp"); // 未登录，跳转到登录页面
            return;
        }

        // 获取表单提交的参数
        String name = request.getParameter("name");
        String description = request.getParameter("description");
        String startTime = request.getParameter("start_time");
        String endTime = request.getParameter("end_time");
        String location = request.getParameter("location");
        String capacityStr = request.getParameter("capacity");

        if (name == null || startTime == null || endTime == null || location == null || capacityStr == null) {
            response.sendRedirect(request.getContextPath() + "/error.jsp"); // 参数缺失，跳转到错误页面
            return;
        }

        int capacity;
        try {
            capacity = Integer.parseInt(capacityStr);
        } catch (NumberFormatException e) {
            response.sendRedirect(request.getContextPath() + "/error.jsp");
            return;
        }

        // 数据库操作
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "INSERT INTO events (name, description, start_time, end_time, location, capacity, created_by, status) " +
                    "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, description);
            ps.setString(3, startTime);
            ps.setString(4, endTime);
            ps.setString(5, location);
            ps.setInt(6, capacity);
            ps.setInt(7, userId);
            ps.setString(8, "报名中"); // 初始状态为 "报名中"
            ps.executeUpdate();

            // 创建成功，返回活动列表页面
            response.setContentType("text/html; charset=UTF-8");
            response.getWriter().println("<script>alert('活动创建成功！'); location.href='" + request.getContextPath() + "/organizer/dashboard';</script>");
        } catch (SQLException e) {
            throw new ServletException(e);
        } finally {
            DBUtil.close(conn, ps, null);
        }
    }
}

